<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
<title>链接角色许可</title>
<link rel="stylesheet" type="text/css" href="../easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="../easyui/demo/demo.css">
<script type="text/javascript" src="../easyui/jquery.min.js"></script>
<script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../uploadjs/vendor/jquery.ui.widget.js"></script>
<script type="text/javascript" src="../uploadjs/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="../uploadjs/jquery.fileupload.js"></script>
<script type="text/javascript" src="../js/auth.js"></script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',border:false" style="height:100px;background:#B3DFDA;padding:10px">
<div style="float:right;clear:both;margin-top:15px;margin-right:20px">
    <a href="javascript:logout()">注销</a>
    </div>
    <h2>蛋糕商城</h2>
    <h3>从2025为公众服务</h3>
</div>
<div data-options="region:'west',split:true,title:'主菜单'" style="width:180px;padding:0px;">
<div onclick="window.location='../pages/index.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">主页</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../admin/index'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">蛋糕商城后台</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/goodses.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">货品</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/custorders.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">订单</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/orderitems.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">订单项</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/recommends.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">推荐</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/types.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">类型</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/roles.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">角色</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/permissions.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">许可</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/users.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">用户</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/profile.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">用户资料</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/linkcustorderorderitem.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">链接订单订单项</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/linkrolepermission.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">链接角色许可</div><div class="menu-icon icon-add"></div></div>
<div onclick="window.location='../pages/linkuserrole.html'" class="menu-item" style="height: 20px;"><div class="menu-text" style="height: 20px; line-height: 20px;">链接用户角色</div><div class="menu-icon icon-add"></div></div>
</div>
<div data-options="region:'east',split:true,collapsed:true,title:'属性'" style="width:250px;overflow: hidden">
</div>
<div data-options="region:'south',border:false" style="height:40px;background:#A9FACD;padding:10px;text-align: center">
    蛋糕商城
</div>
<div data-options="region:'center',title:'第三代动词算子式代码生成器：光　生成结果'">
<table>
<tr>
<td width="80px">
</td>
<td>
<div class="easyui-datalist" title="活跃角色" id="myRole" style="width:200px;height:550px;float:left;display:inline;margin:0px" data-options="
url: '../roleController/listActiveRoles',
method: 'post',
valueField:'id',
textField:'roleName',
idField:'id',
onCheck:checkMyRow,
onLoadSuccess:loadSuccess
">
</div>
</td>
<td>
<div class="easyui-datalist" title="现有许可" id="myPermissions" style="width:200px;height:550px;float:left;display:inline;margin:0px" data-options="
url: '../roleController/listActiveRolePermissionsUsingRoleId',
method: 'post',
valueField:'id',
textField:'permName',
singleSelect:false
">
</div>
</td>
<td>
<div style="float:left;display:inline-block">
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="assignPermissionsToRole()">&lt;&lt;</a><br/><br/><br/>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="revokePermissionsFromRole()">&gt;&gt;</a>
</div>
<div class="easyui-datalist" title="可添加许可" id="myAvailablePermissions" style="width:200px;height:550px;float:left;display:inline;margin:0px" data-options="
url: '../roleController/listAvailableActiveRolePermissionsUsingRoleId',
method: 'post',
valueField:'id',
textField:'permName',
singleSelect:false
">
</div>
</td>
</tr>
</table>
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
checkAccessRole();
});
var currentIndex = 0;
function checkMyRow(index,row){
    var roleId;
    if (row) {
    roleId = row.id;
    currentIndex = index;
    }
    $('#myPermissions').datalist('load',{
    roleId: roleId
    });
    $('#myAvailablePermissions').datalist('load',{
    roleId: roleId
    });
}

function loadSuccess(){
    $("#myRole").datalist("checkRow",currentIndex);
}

function checkAccessRole(){
    $.ajax({
        type: "get",
        url: "../roleController/checkAccessRole",
        dataType: 'json',
        success: function(data, textStatus) {
            if (!data.success && data.noAuth){
                window.location.href="../login/noauth.html"
            }
        },
        complete : function(XMLHttpRequest, textStatus) {
        },
        error : function(XMLHttpRequest,textStatus,errorThrown) {
            alert("Error:"+textStatus);
            alert(errorThrown.toString());
        }
    }); 
}

function assignPermissionsToRole(){
    var permissionIds = new Array();
    var rows = $("#myAvailablePermissions").datalist("getSelections");
    if (isBlank(rows)) {
    $.messager.alert("操作提示", "请选择可添加Permission！");
    return;
    }
    for (var i = 0;i<rows.length;i++){
    permissionIds[i] = rows[i]["id"]; 
    }
    var roleId = $("#myRole").datalist("getSelected").id;
    $.ajax({
    type: "post",
    url: "../roleController/assignPermissionsToRole",
    data: {
    permissionIds:permissionIds.join(","),
    roleId:roleId,
    },
    dataType: 'json',
    success: function(data, textStatus) {
    if (data.success) $("#myRole").datalist("load");
    },
    complete : function(XMLHttpRequest, textStatus) {
    },
    error : function(XMLHttpRequest,textStatus,errorThrown) {
    alert("Error:"+textStatus);
    alert(errorThrown.toString());
    }
    });
}

function revokePermissionsFromRole(){
    var permissionIds = new Array();
    var rows = $("#myPermissions").datalist("getSelections");
    if (isBlank(rows)) {
    $.messager.alert("操作提示", "请选择现有Permission！");
    return;
    }
    for (var i = 0;i<rows.length;i++){
    permissionIds[i] = rows[i]["id"]; 
    }
    var roleId = $("#myRole").datalist("getSelected").id;
    $.ajax({
    type: "post",
    url: "../roleController/revokePermissionsFromRole",
    data: {
    permissionIds:permissionIds.join(","),
    roleId:roleId,
    },
    dataType: 'json',
    success: function(data, textStatus) {
    if (data.success) $("#myRole").datalist("load");
    },
    complete : function(XMLHttpRequest, textStatus) {
    },
    error : function(XMLHttpRequest,textStatus,errorThrown) {
    alert("Error:"+textStatus);
    alert(errorThrown.toString());
    }
    });
}

function isBlank(val){
    if (val == undefined|| val == null || val == "" || val== "0" || val == 0) return true;
    else return false;
}

</script>
</html>
